﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SonCa.SocialNetwork.Web.Accounts.IPresenters;
using SonCa.SocialNetwork.Web.Accounts.Presenters;
using Core.DataAccess;

namespace SonCa.SocialNetwork.Web.Accounts.ASCX
{
    public partial class EditProfile : System.Web.UI.UserControl, IProfileEdit
    {
        private ProfileEditPresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new ProfileEditPresenter();
            _presenter.Init(this);

            if (!IsPostBack)
            {
                _presenter.LoadProfileInfo();
                _presenter.LoadEducation();
                _presenter.LoadWorkPlace();
                _presenter.LoadLocation();
                
                BindCityList();
                LoadGradeList();
                BindYear();
                BindJobField();
            }
        }

        private void BindJobField()
        {
            ddlJobField.DataSource = _presenter.GetJobFieldList();
            ddlJobField.DataBind();
        }

        private void BindYear()
        {
            List<int> yearList = new List<int>();
            for (int i = 1970; i <= DateTime.Now.Year; i++)
            {
                yearList.Add(i);
            }
            yearList.Reverse();
            
            ddlStartYear.DataSource = yearList;
            ddlStartYear.DataBind();

            ddlGraduationYear.DataSource = yearList;
            ddlGraduationYear.DataBind();
        }

        private void BindCityList()
        {
            List<string> cities = _presenter.GetVietnameseCityList();
            
            ddlSchoolCity.DataSource = cities;
            ddlSchoolCity.DataBind();

            ddlCompanyCity.DataSource = cities;
            ddlCompanyCity.DataBind();

            ddlLocationCity.DataSource = cities;
            ddlLocationCity.DataBind();
        }        

        public void LoadProfileInfo(Account account)
        {
            if (account != null)
            {
                string name = string.Empty;
                if (string.IsNullOrEmpty(account.MidName))
                {
                    name = account.LastName + " " + account.FirstName;
                }
                else
                {
                    name = account.LastName + " " + account.MidName + " " + account.FirstName;
                }
                txtFullName.Value = name;
                //txtBirthDate.Value = string.Format("{0: dd/MM/yyyy}", account.BirthDate);
                txtBirthDate.Value = Convert.ToDateTime(account.BirthDate).ToShortDateString();
                txtEmail.Value = account.Email;
                txtAboutMe.Value = account.AboutMe;
                txtInterests.Value = account.Interests;
            }
        }

        public void LoadEducation(List<AccountSchool> accSchoolList,List<School> schoolList)
        {
            if (accSchoolList != null && schoolList != null && accSchoolList.Count > 0 && schoolList.Count > 0)
            {
                AccountSchool accSchool = accSchoolList[0];
                School school = schoolList[0];
                txtSchoolName.Value = school.SchoolName;
                ddlGrade.SelectedValue = school.Grade.ToString();
                ddlSchoolCity.SelectedValue = school.City;
                ddlStartYear.SelectedValue = accSchool.StartYear.ToString();
                ddlGraduationYear.SelectedValue = accSchool.GraduationYear.ToString();
            }
        }

        public void LoadWorkPlace(List<AccountWorkPlace> accwpList,List<WorkPlace> wpList)
        {
            if (accwpList != null && wpList != null && accwpList.Count > 0 && wpList.Count > 0)
            {
                AccountWorkPlace itemAccWp = accwpList[0];
                WorkPlace itemWp = wpList[0];
                txtCompanyName.Value = itemWp.CompanyName;
                txtCompanyAddress.Value = itemWp.Address;
                ddlCompanyCity.SelectedValue = itemWp.City;
                ddlJobField.SelectedValue = itemWp.JobField;
                txtPosition.Value = itemAccWp.Position;
                txtStartDate.Value = Convert.ToDateTime(itemAccWp.StartDate).ToString("dd/MM/yyyy");
                txtEndDate.Value = Convert.ToDateTime(itemAccWp.EndDate).ToString("dd/MM/yyyy");
            }
        }

        public void LoadLocation(List<AccountLocation> accLocationList, List<Location> locationList)
        {
            if (accLocationList != null && locationList != null && accLocationList.Count > 0 && locationList.Count > 0)
            {
                AccountLocation itemAccLo = accLocationList[0];
                Location itemLo = locationList[0];
                txtLocationName.Value = itemLo.LocationName;
                txtLocationAddress.Value = itemLo.Address;
                ddlLocationCity.SelectedValue = itemLo.City;
                txtLocationStartDate.Value = Convert.ToDateTime(itemAccLo.StartDate).ToString("dd/MM/yyyy");
                txtLocationEndDate.Value = Convert.ToDateTime(itemAccLo.EndDate).ToString("dd/MM/yyyy");
            }
        }

        public void LoadGradeList()
        {
            ddlGrade.DataSource = _presenter.GetGradeList();
            ddlGrade.DataTextField = "Text";
            ddlGrade.DataValueField = "Value";
            ddlGrade.DataBind();
        }

        public void ChangeViewToggle(int index)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "changeToggle", "changeContent(" + index + ");", true);
        }
    }
}